/*
 * Copyrignt (c) xuzeshui.com. All Rights Reserved.
 * Author: Xiaobao Zeng<zengxiaobao@skyworth.com>
 * Created Time: 2016-07-07 14:21:09
 * Last Modified: 2016-07-20 11:17:41
 * File Name: servicepageinfomgr.go
 * Description:
 */

package controllers

import (
	"encoding/json"
	//	"fmt"

	"advertoper/errorcode"
	"advertoper/servicepage"

	log "github.com/cihub/seelog"
)

type ServicePageInfoMgrController struct {
	SCLoginedController
}

func (p *ServicePageInfoMgrController) CreateServicePageInfo() {
	defer p.Output("CreateServicePageInfo")
	body := p.Ctx.Input.RequestBody
	log.Debugf("recv body %s", string(body))

	req := &servicepage.ServicePageInfoItem{}

	if err := json.Unmarshal(body, &req); err != nil {
		p.Resp["code"] = errorcode.EC_GL_INVALID_JSON_BODY
		p.Resp["msg"] = errorcode.Msg(errorcode.EC_GL_INVALID_JSON_BODY)
		return
	}

	code, msg := servicepage.GetServicePageInfoService().CreateServicePageInfo(req)
	p.Resp["code"] = code
	p.Resp["msg"] = msg
}

func (p *ServicePageInfoMgrController) GetServicePageInfoList() {
	defer p.Output("GetServicePageInfoList")
	list, _ := servicepage.GetServicePageInfoService().ServicePageInfoListMgr(0, 10000, nil, false)
	p.Resp["code"] = errorcode.EC_GL_SUCCESS
	p.Resp["msg"] = errorcode.Msg(errorcode.EC_GL_SUCCESS)
	p.Resp["data"] = list
}

func (p *ServicePageInfoMgrController) DeleteServicePageInfo() {
	defer p.Output("DeleteServicePageInfo")
	body := p.Ctx.Input.RequestBody
	log.Debugf("recv body %s", string(body))

	bodyData := make(map[string]interface{})
	if err := json.Unmarshal(body, &bodyData); err != nil {
		p.Resp["code"] = errorcode.EC_GL_INVALID_JSON_BODY
		p.Resp["msg"] = errorcode.Msg(errorcode.EC_GL_INVALID_JSON_BODY)
		return
	}

	var sid int64
	if val, ok := bodyData["sid"]; ok {
		sid = int64(val.(float64))
	} else {
		p.Resp["code"] = errorcode.EC_SERVICEPAGEINFO_SID_MISSING
		p.Resp["msg"] = errorcode.Msg(errorcode.EC_SERVICEPAGEINFO_SID_MISSING)
		return
	}
	code, msg := servicepage.GetServicePageInfoService().DeleteServicePageInfo(sid)
	p.Resp["code"] = code
	p.Resp["msg"] = msg
}
